<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of soaUsers
 *
 * @author dxx
 */

namespace app\common\model\jooj_member;

use think\Model;
use app\common\common\Helper;


class Base extends Model {

    protected $newArr = [];

    /**
     * 构造方法
     * @param type $data
     */
    public function __construct($data = []) {
        parent::__construct($data);
    }

    /**
     * 根据模板创建数据表
     * @param $table string 数据表名
     * @param $baseTable string 数据基表名
     * @return type true
     */
    protected function createTable($table,$baseTable) {

        # 思路:判断表是否存在,若表不存在,则创建表,否则返回true退出,
        try {           
            
            $this->checkTable($table);
        } catch(\Exception $e) {

            try {
                 $sql = 'CREATE TABLE ' . $table . ' LIKE ' . $baseTable;
             $this->execute($sql);

            } catch (\Exception $e) {
                return true;  //表已经存在
            }
        }
       
    }
    /**
     * 判断数据表是否存在
     * @param type $table 数据表名
     * @return type 执行结果
     */
    protected function checkTable($table) {
        $sql = 'SHOW TABLES LIKE '. $table;
        //$sql = 'CREATE TABLE IF NOT EXISTS '. $table;
        return $this->execute($sql);
    }
    /**
     * 获取分页数据和count
     * @param type $where
     * @param type $page
     * @param type $pageSize
     * @param type $field
     * @return type
     */
    public function getPageCount($where, $page = 1, $pageSize = 10, $field = "*", $order = '') {
        $query = !empty($where) ? $this->where($where) : $this;
        $count = $query->count();
        if ($isCount) {
            return $count;
        }
        if ($where) {
            $query = $this->where($where);
        }
        $query = $query->page($page, $pageSize);
        if (!empty($order)) {
            $query = $query->order($order);
        }
        $list = $query->column($field);
        $data = [
            'totalCount' => $count,
            'totalPage'  => $count ? ceil($count / $pageSize) : 0,
            'page'       => $page,
            'pageSize'   => $pageSize,
            'list'       => array_values(Helper::convertUnderlineArray($list)),
        ];
        return $data;
    }

    /**
     * 下划线转成驼峰
     * @param type $arr
     * @param type $status
     * @param type $key
     * @param type $ucfirst
     * @return boolean
     */
    public function convertUnderline($arr) {
        if (!is_array($arr)) {
            return false;
        }
        foreach ($arr as $k => $value) {
            if (!is_array($value)) {
                $str = ucwords(str_replace('_', ' ', $k));
                $str = str_replace(' ', '', lcfirst($str));
                $str = $ucfirst ? ucfirst($str) : $str;
                $this->newArr[$str] = $value;
            } else {
                unset($this->newArr[$str]);
                $this->convertUnderline($value, false, $str);
            }
        }
        return $this->newArr;
    }

    /**
     * 驼峰转下划线(暂时只支持一维数组)
     * @return $array
     */
    public function toUnderScore($arr) {
        if (!is_array($arr)) {
            return false;
        }
        foreach ($arr as $key => $value) {
            $newKey = strtolower(preg_replace('/((?<=[a-z])(?=[A-Z]))/', '_', $key));
            $this->newArr[$newKey] = $value;
        }
        return $this->newArr;
    }

}
